﻿Public Class SelectServerControl
    Public Property SelectedServer As String

    Dim ServerNames As New Dictionary(Of String, String)

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Try
            EndpointAdr = New ServiceModel.EndpointAddress(SelectedServer)
        Catch
            MsgBox("The Adress is incorrect, please try again")
            Exit Sub
        End Try
        OK_Button.Enabled = False
        RefreshButton.Enabled = False
        Servers.Enabled = False
        MainForm.Connect()
    End Sub

    Private Sub SelectServerDialog_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RefreshServers()
        If FirstPlay Then
            TutorialControl1.ShowAndMoveOn()
        End If
    End Sub

    Dim WithEvents discoveryClient As New ServiceModel.Discovery.DiscoveryClient("udpDiscovery")
    Sub RefreshServers()
        Invoke(New Action(AddressOf ShowLoading))
        Dim findCriteria As New ServiceModel.Discovery.FindCriteria(GetType(Global.MPQClient.Server.IServer))
        discoveryClient.FindAsync(findCriteria)
    End Sub

    Private Sub discoveryClient_FindProgressChanged(ByVal sender As Object, ByVal e As System.ServiceModel.Discovery.FindProgressChangedEventArgs) Handles discoveryClient.FindProgressChanged
        If Servers.Items.Contains("Loading...") Then
            Invoke(New Action(Of Object)(AddressOf Servers.Items.Remove), "Loading...")
        End If
        With (e.EndpointDiscoveryMetadata.Address.Uri)
            Dim ServerName As String
            If Not .AbsoluteUri.EndsWith("/Server/") Then
                ServerName = .AbsoluteUri.Remove(.AbsoluteUri.Length - 1)
                ServerName = ServerName.Remove(0, ServerName.LastIndexOf("/") + 1)
                ServerName = ServerName.Replace("_", " ")
            Else
                ServerName = .AbsoluteUri
            End If
            If Not ServerNames.Keys.Contains(ServerName) Then
                ServerNames.Add(ServerName, .AbsoluteUri)
            End If
            Invoke(New Action(Of String)(AddressOf AddItem), ServerName)
        End With
    End Sub

    Sub AddItem(ByVal item As String)
        Servers.Items.Add(item)

        With My.Application.CommandLineArgs
            If .Contains("/s") Then
                If Strings.UCase(item) = Strings.UCase(.Item(.IndexOf("/s") + 1)) Then
                    Servers.SelectedItem = item
                    OK_Button.PerformClick()
                End If
            End If
        End With
    End Sub

    Sub ShowLoading()
        Servers.Items.Clear()
        Servers.Items.Add("Loading...")
    End Sub

    Private Sub Servers_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Servers.SelectedValueChanged
        Try
            SelectedServer = ServerNames(Servers.SelectedItem)
        Catch
        End Try
    End Sub

    Private Sub RefreshButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshButton.Click
        RefreshServers()
    End Sub
End Class
